<?php

namespace app\common\model\Product;

// 引入TP基础模型
use think\Model;

// 订单产品表类模型
class  OrderProduct extends Model
{
    protected $name = 'order_product';

    public function getQueryTimeTextAttr($value, $data)
    {
        $query_time = !empty($data['query_time']) ? $data['query_time'] : '';

        if ($query_time)
        {
            return   date("Y-m-d", $query_time);
        }
        else
        {
            return '';
        }
    }

    public function getQueryQrcordTextAttr($value,$data)
    {
        $url = request()->domain();

        $query_qrcode = !empty($data['query_qrcode']) ? $data['query_qrcode'] : '/assets/img/cover.png';

        return $url .$query_qrcode;
    }

    public $append = [
        'thumbs_list',
        'rate_text',
        'query_time_text', // 查询时间
        'query_qrcord_text', // 二维码
    ];

    public function getRateTextAttr($value, $data) {
        $rate = $data['rate'];
        $rate_text = '';
        if ($rate >= 4) {
            $rate_text = '物超所值！';
        } elseif ($rate >= 3) {
            $rate_text = '与描述相符';
        } else {
            $rate_text = '差评';
        }
        return $rate_text;
    }

    public function getThumbsListAttr($value, $data) {
        if(empty($data['thumbs'])) {
            return [];
        }
        
        $thumbs = explode(',', $data['thumbs']);
        $url = request()->domain();

        foreach ($thumbs as $k => $v) {
            $thumbs[$k] = $url. $v;
        }

        return $thumbs;
    }

    public function product()
    {
        return $this->belongsTo('app\common\model\Product\Product', 'proid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function business() {
        return $this->belongsTo('app\common\model\Business\Business', 'busid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}